import requests
from parsel import Selector


class ChapterCrawler:

    def __init__(self, bid_url):
        self.url = bid_url
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
        }
        self.text = ""
        self.chapter_url = []

    def get_html(self):

        try:
            resp = requests.get(url=self.url, headers=self.headers)
            self.text = resp.text
        except:
            pass

    def parse_html(self):
        selector = Selector(text=self.text)
        result = selector.xpath('//td/a/@href').getall()
        for r in result:
            self.chapter_url.append('https://ognv.shuqi.com' + r)

    def run(self):
        self.get_html()
        self.parse_html()
        return self.chapter_url


if __name__ == '__main__':
    # url = input('输入bid=')
    url = 'https://ognv.shuqi.com/chapter?bid=8867235'
    spider = ChapterCrawler(url)
    chapter_url = spider.run()

